package com.iacrqq.sedaf.application.config;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public abstract class AbstractDefinition {
	private String name;
	private List<Property> propertyList = new LinkedList<Property>();
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public List<Property> getPropertyList() {
		return propertyList;
	}
	
	public void addProperty(Property property)
	{
		propertyList.add(property);
	}
	
	public String getSingleValue(String key)
	{
		for(Property property : propertyList)
		{
			if(property.getName().equals(key))
			{
				if(property.getType() == Property.PropertyTypeEnum.SINGLE_VALUE)
				{
					return property.getValue();
				}
				else
				{
					throw new RuntimeException("Wrong application config file.");
				}
			}
		}
		
		return null;
	}
	
	public List<String> getSingleOrMultiValue(String key)
	{
		for(Property property : propertyList)
		{
			if(property.getName().equals(key))
			{
				if(property.getType() == Property.PropertyTypeEnum.MULTI_VALUES)
				{
					return property.getValueList();
				}
				else
				{
					List<String> valueList = new LinkedList<String>();
					valueList.add(property.getValue());
					return valueList;
				}
			}
		}
		
		return null;
	}
	
	public List<String> getMultiValue(String key)
	{
		for(Property property : propertyList)
		{
			if(property.getName().equals(key))
			{
				if(property.getType() == Property.PropertyTypeEnum.MULTI_VALUES)
				{
					return property.getValueList();
				}
				else
				{
					throw new RuntimeException("Wrong application config file.");
				}
			}
		}
		
		return null;
	}
	
	public List<String> getPropertyKeys()
	{
		List<String> keyList = new LinkedList<String>();
		for(Property property : propertyList)
		{
			keyList.add(property.getName());
		}
		
		return keyList;
	}
	
	public List<String> getPropertyKeysWithFilter(Collection<String> filter)
	{
		List<String> keys = getPropertyKeys();
		keys.removeAll(filter);
		
		return keys;
	}
}
